from pymt import *
import math

class MTTouchAndShowButton(MTImageButton):

  def __init__(self, **kwargs):
    #print "MTTouchAndShowButton constructed"
    kwargs.setdefault('start_angle', 0)
    kwargs.setdefault('end_angle', 360)
    kwargs.setdefault('sub_buttons_radius_extra_spacing', 20)
    
    self.start_angle = kwargs.get("start_angle")
    self.end_angle = kwargs.get("end_angle")
    self.sub_buttons_radius_extra_spacing = kwargs.get("sub_buttons_radius_extra_spacing")
    
    if(self.start_angle >= self.end_angle):
      print "MTTouchAndShowButton: start_angle should be smaller than end_angle."
      self.start_angle = 0
      self.end_angle = 360
    
    self.sub_buttons_radius = 0 # Will be set when sub_buttons are added.
    self.sub_buttons = {}
    
    # example sub_buttons
    #self.add_sub_button("resources/graphics/flickr.png", do_something)
    #self.add_sub_button("resources/graphics/youtube.png", do_something)
    #self.add_sub_button("resources/graphics/dropbox.png", do_something)
    #self.add_sub_button("resources/graphics/picassa.png", do_something)
    #self.add_sub_button("resources/graphics/s.png", do_something)
    super(MTTouchAndShowButton, self).__init__(**kwargs)
    
  def add_sub_button(self, filename, callback_function):
    #print "MTTouchAndShowButton add_sub_button with filename:", filename
    new_sub_button = MTImageButton(filename=filename)
    self.sub_buttons[new_sub_button] = callback_function
    self.sub_buttons_radius = max(self.sub_buttons_radius, max(new_sub_button.width, new_sub_button.height))
    
  def on_press(self, touch):
    #print "MTTouchAndShowButton pressed"
    self.show_sub_buttons()
  
  def on_release(self, touch):
    #print "MTTouchAndShowButton released"
    # check if over a sub_button
    for sub_button in self.sub_buttons.keys():
      if(sub_button.collide_point(touch.x, touch.y)):
        #print "sub_button touched:", sub_button.filename
        self.sub_buttons[sub_button](touch)
    
    self.hide_sub_buttons()
      
  def show_sub_buttons(self):
    #print "Show sub-buttons"
    if(len(self.sub_buttons.keys()) > 0):
      angle = self.start_angle
      if(len(self.sub_buttons.keys()) == 1):
        angle_increment = 0
        angle = (self.end_angle + self.start_angle) / 2.0
      else:
        if(self.end_angle - self.start_angle == 360):
          angle_increment = (self.end_angle - self.start_angle) / float(len(self.sub_buttons))
        else:
          angle_increment = (self.end_angle - self.start_angle) / float(len(self.sub_buttons) - 1)
      for sub_button in self.sub_buttons.keys():
        #print "Showing sub_button"
        x = math.sin(angle * ( math.pi / 180.0) ) * (self.sub_buttons_radius + self.sub_buttons_radius_extra_spacing)
        y = math.cos(angle * ( math.pi / 180.0) ) * (self.sub_buttons_radius + self.sub_buttons_radius_extra_spacing)
        sub_button.pos = ((self.x - (sub_button.width / 2) + (self.width / 2)) + x, self.y + y)

        self.add_widget(sub_button)
        
        angle += angle_increment
  
  def hide_sub_buttons(self):
    #print "Hide sub-buttons"
    for sub_button in self.sub_buttons.keys():
      self.remove_widget(sub_button)
    
def do_something():
  print "njb"

if __name__ == '__main__':


    # Create background window
    w = getWindow()

    button = MTTouchAndShowButton(filename="resources/graphics/usb.png", pos=(w.width / 2, w.height / 2), start_angle = 120, end_angle = 240, sub_buttons_radius_extra_spacing = 50)
    w.add_widget(button)
    
    runTouchApp()
